# -*- coding: utf-8 -*-

import numpy as np
from tensorflow.keras.models import load_model
from 通用 import config
from 数据处理 import 数组加载, csv文件加载, csv文件加载_dataset

if __name__ == '__main__':
    print('---数据加载路径:', config.预测数据)
    print('---模型为: ' + config.model_name_分类)

    # 加载预测数据
    train_dataset, validate_dataset, shape, classnum = csv文件加载_dataset.load(config.训练数据, 3, "纵向", "分类")

    # 加载模型
    model = load_model(config.model_name_分类)

    # 预测
    y = model.predict(validate_dataset)

    # 获取真实值y_true
    y_true = np.array([])
    for batch in validate_dataset:
        inputs, targets = batch
        y_true = np.append(y_true, targets.numpy())

    # 计算涨跌占比
    y_true_1 = 0
    for p in range(len(y_true)):
        if y_true[p] == 1:
            y_true_1 = y_true_1 + 1
    print("\033[34m验证数据涨跌占比:", y_true_1/len(y_true))

    # 获取训练的值
    y_train = np.array([])
    for batch in train_dataset:
        inputs, targets = batch
        y_train = np.append(y_train, targets.numpy())

    # 计算涨跌占比
    y_train_1 = 0
    for p in range(len(y_train)):
        if y_train[p] == 1:
            y_train_1 = y_train_1 + 1
    print("\033[34m训练数据涨跌占比:", y_train_1/len(y_train))

    # array转list打印可以防止自动换行
    y = y.argmax(axis=1).tolist()
    y_true = y_true.astype(int).tolist()
    # 每组30个, 分批计算误差
    batch = 100
    acc_total = 0
    acc_batch = []
    batch_acc = 0
    for i in range(len(y)):
        if y[i] == y_true[i]:
            acc_total = acc_total + 1
            batch_acc = batch_acc + 1
        if i % batch == 0:
            if i != 0:
                acc_batch.append(batch_acc)
            batch_acc = 0

    print("\033[32m预测的值:", y)
    print("\033[32m实际的值:", y_true)

    分批准确率 = ""
    for i in range(len(acc_batch)):
        分批准确率 = 分批准确率 + str(acc_batch[i]/batch * 100) + "%, "
    print("\033[33m分批准确率:", 分批准确率)
    print("\033[33m总准确率:", acc_total/len(y) * 100, "%")

